package com.global.college.views;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import androidx.fragment.app.DialogFragment;

import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alipay.sdk.app.PayTask;
import com.global.college.utils.GooglePlayUtil;
import com.google.gson.Gson;
import com.global.college.CollegeApplication;
import com.global.college.R;
import com.global.college.api.AliPayURLs;
import com.global.college.base.BaseActivity;
import com.global.college.base.PayResult;
import com.global.college.base.baselist.EventBusBean;
import com.global.college.constants.Constant;
import com.global.college.ui.activity.PayFailActivity;
import com.global.college.ui.bean.OrderCreateModel;
import com.global.college.ui.bean.WxPayModel;
import com.global.college.ui.bean.requestBean.AlipayParams;
import com.global.college.ui.bean.requestBean.WxParams;
import com.global.college.ui.interfaces.IPayOrderView;
import com.global.college.ui.presenter.PayOrderPresenter;
import com.global.college.utils.StringUtils;
import com.global.college.utils.ToastUtils;
import com.global.college.widget.FrescoImageView;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import pay.AuthResult;


/**
 * 获取帮买列表
 *
 * @author Administrator
 */
public class PayOrderDialog extends DialogFragment implements IPayOrderView {
    private static final String TAG = "AnchorHandleDialog";
    @BindView(R.id.fiv_icon)
    FrescoImageView fivIcon;
    @BindView(R.id.tv_goods_title)
    TextView tvGoodsTitle;
    @BindView(R.id.tv_goods_price)
    TextView tvGoodsPrice;
    @BindView(R.id.rl_goods_detail)
    RelativeLayout rlGoodsDetail;
    @BindView(R.id.tv_goods_type)
    TextView tvGoodsType;
    @BindView(R.id.tv_total_price)
    TextView tvTotalPrice;
    @BindView(R.id.tv_goods_num)
    TextView tvGoodsNum;
    @BindView(R.id.tv_goods_discount)
    TextView tvGoodsDiscount;
    @BindView(R.id.tv_bottom_price)
    TextView tvBottomPrice;
    @BindView(R.id.ll_bottom_price)
    LinearLayout llBottomPrice;
    @BindView(R.id.tv_bottom_pay)
    TextView tvBottomPay;
    @BindView(R.id.ll_bottom_pay)
    LinearLayout llBottomPay;
    @BindView(R.id.ll_tip_title)
    RelativeLayout llTipTitle;
    @BindView(R.id.rl_dialog)
    LinearLayout rlDialog;
    @BindView(R.id.mViewDis)
    View mViewDis;
    Unbinder unbinder;
    private PayOrderPresenter payOrderPresenter;
    private OrderCreateModel orderCreateModel;
    private String trainId;
    /**
     * buyForWho
     * 1.自己给自己买
     * 2.自己给别人买
     * 3.请别人帮自己买
     */
    private String buyForWho = "1";

    /**
     * 上下文
     */
    private BaseActivity mActivity;

    private View view;

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.tv_bottom_pay, R.id.mViewDis})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_bottom_pay:
                if (null != orderCreateModel) {
                    // TODO: 2024/7/18 调用google支付
                    GooglePlayUtil.getInstance().callGooglePay(orderCreateModel.getGoodId(), orderCreateModel.getOrderId());
                }
                dismiss();
                break;
            case R.id.mViewDis:
                dismiss();
                break;
        }
    }

    @Override
    public void onGoogleResumeSuccess(String object) {
        dismiss();
        mActivity.finish();
    }

    @Override
    public void onGooglePayFail(String error) {
        mActivity.JugeAndOpenActivity(PayFailActivity.class);
    }

    public interface OnCallBack {
        void callBack(String param);
    }

    private static OnCallBack mOnCallBack;

    public static PayOrderDialog newInstance(String trainId, OrderCreateModel model, OnCallBack onCallBack) {
        Bundle args = new Bundle();
        args.putString("trainId", trainId);
        args.putSerializable("model", model);
        mOnCallBack = onCallBack;
        PayOrderDialog sDialog = new PayOrderDialog();
        sDialog.setArguments(args);
        return sDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.BottomDialog);
        payOrderPresenter = new PayOrderPresenter(getActivity());
        payOrderPresenter.setPayView(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().setCanceledOnTouchOutside(true);
        mActivity = (BaseActivity) getActivity();
        EventBus.getDefault().register(this);
        view = View.inflate(mActivity, R.layout.dialog_pay_order, null);
        unbinder = ButterKnife.bind(this, view);

        //初始化布局
        initView();
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.BOTTOM;
        window.setAttributes(params);
    }

    /**
     * 初始化布局
     *
     * @param
     */
    private void initView() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            trainId = bundle.getString("trainId", "");
            orderCreateModel = (OrderCreateModel) bundle.getSerializable("model");
        }

        fivIcon.setImageURI(orderCreateModel.getGoodCover());
        tvGoodsTitle.setText(orderCreateModel.getGoodName());
        tvGoodsPrice.setText("¥" + StringUtils.getPrice2(String.valueOf(orderCreateModel.getGoodPrice())));
        //商品类型，1课程2训练营
        switch (orderCreateModel.getGoodType()) {
            case "1":
                tvGoodsType.setText("课程");
                break;
            case "2":
                tvGoodsType.setText("训练营");
                break;
        }
        tvTotalPrice.setText("¥" + StringUtils.getPrice2(String.valueOf(orderCreateModel.getGoodPrice())));
        tvGoodsNum.setText("x1");
        tvGoodsDiscount.setText("¥" + StringUtils.getPrice2(String.valueOf(orderCreateModel.getGoodPreferentialPrice())));
        tvBottomPrice.setText("¥" + StringUtils.getPrice2(String.valueOf(orderCreateModel.getOrderPrice())));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusCallBack(EventBusBean event) {
        if (event != null) {
            switch (event.getType()) {
                case Constant.EventBus.APY_SUCCESS:
                    //支付成功，消耗商品
                    payOrderPresenter.googlePayResume(new AlipayParams());

                    break;
                case Constant.EventBus.APY_FAIL:
                    startActivity(new Intent(mActivity, PayFailActivity.class));
                    dismiss();
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
